package com.ts.api.module.api.entity.vo;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.util.H5DomainUrlUtils;
import com.ts.api.module.api.entity.ApiProtocol;
import com.ts.api.module.api.entity.bo.ApiDataExchangeBO;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeField;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributePhoneMd5Field;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 *  撞库结果-VO实体
 *
 *  @author Pan
 */
@Data
public class RespV2ApiDataExchangeVO {

    /** 公司Logo地址 */
    private String companyLogo;
    /** 公司名称 */
    private String companyName;
    /** 产品名称 */
    private String productName;
    /** 跳转url */
    private String url;
    /** 返回我方授权页则返回id(手机号md5)，返回对方授权页则返回对方自定义内容 */
    private String applyId;
    /** 价格 */
    private BigDecimal price;
    /** 协议集合 */
    private List<ApiProtocol> protocolList;

    public RespV2ApiDataExchangeVO() {
        super();
    }

    /**
     *  创建撞库响应
     *
     *  @param   apiDataExchangeBO      撞库业务BO
     *  @param   apiKeyPO               API渠道客户端
     *  @return  RespV2ApiDataExchangeVO
     */
    public static RespV2ApiDataExchangeVO create(ApiDataExchangeBO apiDataExchangeBO, ApiKeyPO apiKeyPO) {
        String apiClientId = apiKeyPO.getApiClientId();
        PlanDistributeField planDistributeField = apiDataExchangeBO.getPlanDistributeField();
        PlanDistributePhoneMd5Field phoneMd5Field = planDistributeField.getPhoneMd5Field();

        RespV2ApiDataExchangeVO respV2ApiDataExchangeVO = new RespV2ApiDataExchangeVO();
        respV2ApiDataExchangeVO.setCompanyName(phoneMd5Field.getProductName());
        respV2ApiDataExchangeVO.setProductName(phoneMd5Field.getDisplayName());
        respV2ApiDataExchangeVO.setCompanyLogo(phoneMd5Field.getLogoPath());
        respV2ApiDataExchangeVO.setPrice(phoneMd5Field.getPrice());

        //  回调授权页面-如果匹配非平台则使用自己的回调授权页，如果匹配平台计划则返回的是下游授权页面
        if (ValidParam.isEmpty(phoneMd5Field.getCallUrl())) {
            respV2ApiDataExchangeVO.setApplyId(phoneMd5Field.getPhoneMd5());
            respV2ApiDataExchangeVO.setUrl(H5DomainUrlUtils.createH5CallUrl(phoneMd5Field.getPhoneMd5(), apiClientId));
        } else {
            respV2ApiDataExchangeVO.setApplyId(phoneMd5Field.getApplyId());
            respV2ApiDataExchangeVO.setUrl(phoneMd5Field.getCallUrl());
        }

        //  隐私协议对象
        ApiProtocol userProtocolVO = new ApiProtocol();
        userProtocolVO.setProtocolName("《个人信息授权协议》");
        userProtocolVO.setProtocolUrl("https://axh.rongyiudai.com/h5ViewProtocol?uniqueCode=1fc1379861b34ef0898d7864c33b18e2");
        respV2ApiDataExchangeVO.setProtocolList(CollUtils.newArrayList(userProtocolVO));
        return respV2ApiDataExchangeVO;
    }
}
